<?php
# @Author: yongleicheng <jetox>
# @Date:   2017-11-21T02:58:03+08:00
# @Email:  admin@weiqisheng.cn
# @Project: WQSCMS
# @Filename: App.php
# @Last modified by:   jetox
# @Last modified time: 2017-11-21T03:14:51+08:00
# @Copyright: 微企胜网络科技有限公司




namespace app\api\controller;

use think\Controller;
use clt\Easemob;

class App extends Controller
{
    // 注册用户
    public function createUser($mobile, $password, $username)
    {
        $options = config('options');
        $easemob = new Easemob($options);
        $result=$easemob->createUser($mobile, $password);
        if ($result['entities']) {
            $map['mobile'] = $mobile;
            $map['username'] = $username;
            $map['password'] = $password;
            $map['reg_time'] = time();
            $map['token'] = md5(time().mt_rand(1, 99999));
            $id = db('users')->insertGetId($map);
            if ($id === false) {
                $data = array('code'=>'0','msg'=>'注册失败');
            } else {
                $data = array('code'=>'1','msg'=>'注册成功');
            }
        } else {
            $data = array('code'=>'0','msg'=>'注册失败');
        }
        return json($data);
    }
    // 修改用户信息
    public function User_edit($id=null)
    {
        $user = db('users');
        $data=input('post.');
        if (empty($data['password'])) {
            unset($data['password']);
        }
        if ($user->update($data)!==false) {
            $result['msg'] = '会员修改成功!';
            $result['code'] = 1;
        } else {
            $result['msg'] = '会员修改失败!';
            $result['code'] = 0;
        }
    }
    // 用户登录
    public function login($mobile, $password)
    {
        $table = db('users');
        if (!$mobile || !$password) {
            $data = array('code'=>0,'msg'=>'请填写账号或密码');
        }
        $user = $table->where("mobile", $mobile)->find();
        if (!$user) {
            $data = array('code'=>0,'msg'=>'账号不存在!');
        } elseif ($password != $user['password']) {
            $data = array('code'=>0,'msg'=>'密码错误!');
        } elseif ($user['is_lock'] == 1) {
            $data = array('code'=>0,'msg'=>'账号异常已被锁定！！！');
        } else {
            $data = array('code'=>1,'msg'=>'成功登录','info'=>$user);
        }
        return json($data);
    }
    // 获取所有用户信息
    public function userlist($page=null)
    {
        $table = db('users');
        $list = $table->order('id desc')->paginate(10);
        if ($list) {
            $data = array('code'=>1,'data'=>$list);
        }
        return json($data);
    }

    //根据手机号获取账户信息
    public function getuserinfobyMOBILE($mobile)
    {
        $table = db('users');
        $user = $table->where("mobile", $mobile)->find();
        return json($user);
    }
    // 获取新闻列表  可以传进来page参数
    public function GetNewsList($catid=null)
    {
        $table = db('article');
        $map['catid']=$catid;
        $list=$table->where($map)->paginate(10);
        $data = array('code'=>1,'msg'=>'获取数据成功','data'=>$list);
        return json($list);
    }
    //判断是否在环信平台上线
    public function isOnline($mobile)
    {
        $options = config('options');
        $easemob = new Easemob($options);
        $result = $easemob->isOnline($mobile);
        return $result['data'][$mobile];
    }
}
